package com.byd.modules.biz.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byd.common.exception.RRException;
import com.byd.modules.biz.dao.CarModelDao;
import com.byd.modules.biz.dao.TbFlagDao;
import com.byd.modules.biz.dao.UserDictDao;
import com.byd.modules.biz.entity.CarModelEntity;
import com.byd.modules.biz.entity.SuperviseREntity;
import com.byd.modules.biz.service.CarModelService;
import com.byd.modules.biz.service.ExcelService;
import com.byd.modules.biz.service.UserDictService;

@Service("carModelService")
public class CarModelServiceImpl implements CarModelService {
	@Autowired
	private CarModelDao carModelDao;

	@Autowired
	private ExcelService excelService;
	
	@Autowired
	private UserDictService userDictService;

	@Autowired
	private TbFlagDao tbFlagDao;

	//@Autowired
	//private UserDictDao userDictDao;

	@Override
	public CarModelEntity queryObject(Long id) {
		return carModelDao.queryObject(id);
	}

	@Override
	public List<CarModelEntity> queryList(Map<String, Object> map) {
		return carModelDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return carModelDao.queryTotal(map);
	}

	@Override
	@Transactional
	public void save(CarModelEntity dimension) {
		CarModelEntity parentDim = this.carModelDao.queryObject(dimension.getParentId());
		dimension.setLevel(parentDim != null ? parentDim.getLevel() + 1 : 1);
		if (dimension.getLevel() == 3) {
			if (carModelDao.queryUnique(dimension.getName()) > 0) {
				throw new RRException("品牌库已经存在此名称或别名的车型！");
			}
		}
		carModelDao.save(dimension);
		dimension.setLinkId(
				parentDim != null ? parentDim.getLinkId() + dimension.getId() + "_" : "_" + dimension.getId() + "_");
		dimension.setLinkName(parentDim != null ? parentDim.getLinkName() + dimension.getName() + "_"
				: "_" + dimension.getName() + "_");
		dimension.setParentName(parentDim != null ? parentDim.getName() : "");
		carModelDao.update(dimension);

		//userDictDao.saveWord(dimension.getName());
		userDictService.saveName(dimension.getName());
		if (StringUtils.isNotBlank(dimension.getNickname())) {
			userDictService.saveNickName(dimension.getNickname());
			//userDictDao.saveWord(dimension.getNickname());
		}
		tbFlagDao.updateCount(3);
	}

	@Override
	@Transactional
	public void update(CarModelEntity dimension) {
		CarModelEntity parentDim = this.carModelDao.queryObject(dimension.getParentId());
		dimension.setLinkId(
				parentDim != null ? parentDim.getLinkId() + dimension.getId() + "_" : "_" + dimension.getId() + "_");
		dimension.setLinkName(parentDim != null ? parentDim.getLinkName() + dimension.getName() + "_"
				: "_" + dimension.getName() + "_");
		dimension.setLevel(parentDim != null ? parentDim.getLevel() + 1 : 1);
		dimension.setParentName(parentDim != null ? parentDim.getName() : "");
		carModelDao.update(dimension);

		userDictService.saveName(dimension.getName());
		if (StringUtils.isNotBlank(dimension.getNickname())) {
			
			userDictService.saveNickName(dimension.getNickname());		
			// userDictDao.saveWord(dimension.getNickname());
		}
		tbFlagDao.updateCount(3);
	}

	@Override
	public void delete(Long id) {
		carModelDao.delete(id);
	}

	@Override
	public void deleteBatch(Long[] ids) {
		carModelDao.deleteBatch(ids);
	}

	/**
	 * 重载方法
	 * 
	 * @param parentId
	 * @return
	 */
	@Override
	public List<CarModelEntity> queryListFilter(Long parentId) {
		return carModelDao.queryListFilter(parentId);
	}

	/**
	 * 重载方法
	 * 
	 * @param parentId
	 * @param response
	 */
	@Override
	public void export(Map query, HttpServletResponse response) {
		List<CarModelEntity> queryList = carModelDao.queryListTree(query);
		for (CarModelEntity dimensionEntity : queryList) {
			dimensionEntity.setName(dim2Space(dimensionEntity.getLevel(), dimensionEntity.getName()));
		}
		response.setHeader("content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment;filename=CarModel.xls");
		excelService.exportExcel(new ExportParams(), CarModelEntity.class, queryList, response);
	}

	public String dim2Space(int level, String name) {
		String newName = "";
		for (int i = 1; i < level; i++) {
			newName += "  ";
		}
		return newName + name;
	}

	@Override
	public List<CarModelEntity> listFilterByName(String name) {
		return carModelDao.queryListFilterByName(name);
	}

	@Override
	public void upNicknameBySuperviseSub(SuperviseREntity superviseSub) {
		if (StringUtils.isBlank(superviseSub.getCar_nickname())) {
			return;
		}
		String name = "";
		if (StringUtils.isNotBlank(superviseSub.getBrand())) {
			name = superviseSub.getBrand();
		}
		if (StringUtils.isNotBlank(superviseSub.getSeries())) {
			name = superviseSub.getSeries();
		}
		if (StringUtils.isNotBlank(superviseSub.getModel_num())) {
			name = superviseSub.getModel_num();
		}
		if (StringUtils.isBlank(name)) {
			return;
		}
		CarModelEntity car = carModelDao.queryObjectByName(name);
		if (car == null) {
			return;
		}
		if (car.getNickname() == null || car.getNickname().indexOf(superviseSub.getCar_nickname()) == -1) {
			CarModelEntity carUp = new CarModelEntity();
			carUp.setId(car.getId());
			if (car.getNickname() == null) {
				carUp.setNickname(superviseSub.getCar_nickname());
			} else if (car.getNickname().trim().length() > 0) {
				carUp.setNickname(car.getNickname() + ";" + superviseSub.getCar_nickname());
			} else {
				carUp.setNickname(superviseSub.getCar_nickname());
			}
			carModelDao.update(carUp);
		}
	}

}
